﻿using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Craft;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Technologys
{
    /// <summary>
    /// 工序行显示
    /// </summary>
    public class OperationLinePageHandler : IRequestHandler<OperationLinePageCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 工序行信息
        /// </summary>
        private readonly IBaseRepositoryGet<OperationLine> operationLineRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<OperationLinePageHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="operationLineRepository"></param>
        /// <param name="logger"></param>
        public OperationLinePageHandler(IBaseRepositoryGet<OperationLine> operationLineRepository, ILogger<OperationLinePageHandler> logger)
        {
           this.operationLineRepository = operationLineRepository;
           this.logger = logger;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(OperationLinePageCommand request, CancellationToken cancellationToken)
        {
            var result= new Result() { Code=(int)CommonErrorCodes.Search};

            try
            {
                string sql = $"select a.SerialNumber,a.LeadTime,a.LabourPrice,LostButton,a.OperationStatus,a.Handle,a.HandleTime ,b.WorkCenterId,b.WorkCenterName,c.ProcessId,c.ProcessName from OperationLine a inner join workCenters b on a.WorkCenterId=b.WorkCenterId join Process c on a.ProcessId=c.ProcessId  where ProcessRouteId={request.ProcessRouteId}";
                OperationLineDto lineDto=new OperationLineDto();
                var list = operationLineRepository.GetAll(sql, lineDto);
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "显示成功";
                    logger.LogInformation("工作中心的工序行信息显示成功");
                }
                else
                {
                    result.Msg = "显示失败";
                }

            }
            catch (Exception ex)
            {

                logger.LogError("工作中心的工序行信息显示失败" + ex.Message);
                throw;
            }

            return result;
        }
    }
}
